public class Jump {
    // leetcode 跳跃游戏
    // https://leetcode.cn/problems/jump-game-ii/description/?envType=study-plan-v2&envId=top-100-liked
    public int jump(int[] nums) {
        int len = nums.length;
        int[] dp = new int[len];
        for (int i = 1; i < len; i++) {
            dp[i] = 0x3f3f3f3f;
        }
        for (int i = 1; i < len; i++) {
            for (int j = i-1; j >= 0; j--) {
                if (j + nums[j] >= i) {
                    dp[i] = Math.min(dp[i], dp[j] + 1);
                }
            }
        }
        return dp[len-1];
    }

    /**
     * 贪心
     * @param nums
     * @return
     */
    public int jump1(int[] nums) {
        // step记录跳跃次数，end记录在当前跳跃区间所能到达的最远距离，
        // maxDistance记录跳跃到当前位置之后可以到达的最远距离
        int step = 0, end = 0, maxDistance = 0, len = nums.length;
        for (int i = 0; i < len - 1; i++) {
            maxDistance = Math.max(maxDistance, i + nums[i]);
            if (i == end) {
                step++;
                end = maxDistance;
            }
        }
        return step;
    }

    public static void main(String[] args) {
        Jump jump = new Jump();
        System.out.println(jump.jump(new int[]{2,3,1,1,4}));
    }
}
